﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FlexiCommerce.Components;
using System.Text.RegularExpressions;

namespace FlexiCommerce.Web
{
    public class UrlParser : IUrlParser
    {
        #region Fields

        protected readonly IWebContext webContext;

        #endregion Fields

        #region Constructors

        public UrlParser(IWebContext webContext)
        {
            this.webContext = webContext;
        }

        #endregion Constructors

        #region Properties

        public System.Text.RegularExpressions.Regex CategoryRegex
        {
            get { return new Regex(string.Format("[\\S]*-[\\S]*_[\\d]*", Context.LocalizationManager.GetResource("CATEGORY")), RegexOptions.CultureInvariant); }
        }

        public Category CurrentCategory
        {
            get
            {
                return webContext.CurrentCategory ?? (webContext.CurrentCategory = ParseCategory(webContext.Url));
            }
        }

        #endregion Properties

        #region Methods

        public string BuildUrl(FlexiCommerce.Components.Category item)
        {
            string url = Url.Format("~/" + FlexiCommerce.Context.LocalizationManager.GetResource("CATEGORY") + "-" + item.Name + "_" + item.ID.ToString() + ".aspx");
            return url;
        }

        public string BuildUrl(Category item, System.Globalization.CultureInfo culture)
        {
            string url = Url.Format("~/" + FlexiCommerce.Context.LocalizationManager.GetResource("CATEGORY", culture) + "-" + item.Name + "_" + item.ID.ToString() + ".aspx");
            return url;
        }

        public Category ParseCategory(string url)
        {
            Url u = new Url(url);
            string TrailingPart = Url.GetTrailingPathPart(u.PathWithoutExtension);

            if (!CategoryRegex.IsMatch(TrailingPart))
                return null;
            try
            {
                Category category = Find.Categories.SingleOrDefault(c => c.ID == ParseCategoryIdentifier(TrailingPart));

                return category;
            }
            catch (Exception)
            {

                return null;
            }
            
        }

        public int ParseCategoryIdentifier(string input)
        {
            int index = input.IndexOf("_");
            string id = input.Substring(index + 1);
            return int.Parse(id);
        }

        #endregion Methods

        #region IUrlParser Members


        public string BuildUrl(Product item, System.Globalization.CultureInfo culture)
        {
            string url = Url.Format("~/" + Context.LocalizationManager.GetResource("PRODUCT", culture) + "-" + item.Name + "_" + item.ID.ToString() + ".aspx");
            return url;
        }

        public string BuildUrl(Product item)
        {
            string url = Url.Format("~/" + Context.LocalizationManager.GetResource("PRODUCT") + "-" + item.Name + "_" + item.ID.ToString() + ".aspx");
            return url;
        }

        #endregion
    }
}
